package com.xidian.zhiancloud.service.dangerWorkTicket.WorkManager;

import com.xidian.zhiancloud.dao.RiskAnalysisSecurityMapper;
import com.xidian.zhiancloud.dao.WorkApplicationMapper;
import com.xidian.zhiancloud.dao.WorkApplicationMapperExt;
import com.xidian.zhiancloud.dto.WorkApplicationDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.RiskAnalysisSecurity;
import com.xidian.zhiancloud.model.RiskAnalysisSecurityExample;
import com.xidian.zhiancloud.model.WorkApplication;
import com.xidian.zhiancloud.model.WorkApplicationExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WorkApplicationService {

    @Autowired
    private WorkApplicationMapper workApplicationMapper;

    @Autowired
    private WorkApplicationMapperExt workApplicationMapperExt;

    @Autowired
    private RiskAnalysisSecurityMapper riskAnalysisSecurityMapper;

    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void add(WorkApplicationDTO workApplicationDTO) {
        int result = workApplicationMapperExt.addReturnId(workApplicationDTO.getWorkApplication());
        if(result == 0){
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        int count = 0;
        for(int i = 0; i < workApplicationDTO.getRiskAnalysisSecurities().size(); i++){
            workApplicationDTO.getRiskAnalysisSecurities().get(i).setWorkApplicationId(workApplicationDTO.getWorkApplication().getId());
            riskAnalysisSecurityMapper.insert(workApplicationDTO.getRiskAnalysisSecurities().get(i));
            count ++;
        }
        if(count != workApplicationDTO.getRiskAnalysisSecurities().size()) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public WorkApplicationDTO getById(Long id) {
        WorkApplication workApplication = workApplicationMapper.selectByPrimaryKey(id);
        if(workApplication == null){
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        RiskAnalysisSecurityExample riskAnalysisSecurityExample = new RiskAnalysisSecurityExample();
        riskAnalysisSecurityExample.createCriteria().andWorkApplicationIdEqualTo(id);
        List<RiskAnalysisSecurity> riskAnalysisSecurities = riskAnalysisSecurityMapper.selectByExample(riskAnalysisSecurityExample);
        WorkApplicationDTO workApplicationDTO = new WorkApplicationDTO();
        workApplicationDTO.setWorkApplication(workApplication);
        workApplicationDTO.setRiskAnalysisSecurities(riskAnalysisSecurities);

        return workApplicationDTO;
    }


    public void deleteByIds(long[] ids) {
        for(int i = 0; i < ids.length; i++) {
            RiskAnalysisSecurityExample riskAnalysisSecurityExample = new RiskAnalysisSecurityExample();
            riskAnalysisSecurityExample.createCriteria().andWorkApplicationIdEqualTo(ids[i]);
            riskAnalysisSecurityMapper.deleteByExample(riskAnalysisSecurityExample);
            workApplicationMapper.deleteByPrimaryKey(ids[i]);
        }
    }

//  危险作业申请中没有"改动"的功能，故不设置更改
//    public void patchById(WorkApplicationDTO workApplicationDTO) {
//        WorkApplication workApplication = new WorkApplication();
//        List<RiskAnalysisSecurity> riskAnalysisSecurities = workApplicationDTO.getRiskAnalysisSecurities();
//        int result = workApplicationMapper.updateByPrimaryKeySelective(workApplication);
//        if(result == 0)
//    }


    public List<WorkApplication> getList(Long companyId, Byte stateType, Long workTicketId, String key, int current, int size) {
        WorkApplicationExample workApplicationExample = new WorkApplicationExample();
        WorkApplicationExample.Criteria criteria1 = workApplicationExample.createCriteria();
        WorkApplicationExample.Criteria criteria2 = workApplicationExample.createCriteria();
        criteria1.andCompanyIdEqualTo(companyId);
        criteria1.andCompanyIdEqualTo(companyId);
        // 需要更新数据库
        if(stateType != null){
            criteria1.andStatetypeEqualTo(stateType);
            criteria2.andStatetypeEqualTo(stateType);
        }
        if(workTicketId != null){
            criteria1.andWorkTicketIdEqualTo(workTicketId);
            criteria2.andWorkTicketIdEqualTo(workTicketId);
        }
        if(key != null && !"".equals(key)){
            criteria1.andWorkNameLike('%' + key + '%');
            //location查询
//            criteria2.andlocationLike('%' + key + '%');
            workApplicationExample.or(criteria2);
        }
        return workApplicationMapper.selectByExampleWithRowbounds(workApplicationExample, new RowBounds(current, size));
    }
}





